package org.example.fileupload.service;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @Author zy
 * @Date 2024/12/20 17:10
 * @packageName:org.example.fileupload.service
 * @ClassName:FileService
 */
@Service
public class FileService {
    public void export(HttpServletResponse response) throws IOException {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("templates/文件下载模板.xlsx");
        XSSFWorkbook excel=new XSSFWorkbook(inputStream);

        XSSFSheet sheet = excel.getSheet("sheet1");
        XSSFRow row = sheet.getRow(1);
        row.getCell(0).setCellValue(new Date());
        row.getCell(1).setCellValue("123456");
        row.getCell(2).setCellValue("晴天");
        row.getCell(3).setCellValue("北京");
        row.getCell(4).setCellValue("36.5");

        ServletOutputStream outputStream = response.getOutputStream();
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename=details.xls");
        response.setContentType("application/msexcel");
        excel.write(outputStream);

        outputStream.close();
        excel.close();
    }
}
